package com.sf.osms.dep.processor.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sf.osms.dep.domain.Router;


public class ReflectUtils {
	
	private static Logger logger = LoggerFactory.getLogger(ReflectUtils.class);
	
	/**
	 * 根据属性进行设置
	 * @param obj 需要设置的对象
	 * @param fieldStr  需要设置的对象的属性
	 * @param fieldValue 需要设置的对象的属性的值
	 */
	public static void setFiledValue(Object obj, String fieldStr,
			Object fieldValue) {
		if(obj == null || fieldValue == null || StringUtils.isEmpty(fieldStr)){
			return;
		}
		Class clazz = obj.getClass();
		Field[] Fields = clazz.getDeclaredFields();
		for (Field field : Fields) {
			if(fieldStr.equals(field.getName())){
				String initial = fieldStr.charAt(0)+"";
				fieldStr = fieldStr.replaceFirst(initial,initial.toUpperCase());
				String setMethodName = "set"+ fieldStr;
				try{
					Class fieldClazz = fieldValue.getClass();
					if(fieldValue instanceof List){
						fieldClazz = List.class;
					}else if(fieldValue instanceof Map){
						fieldClazz = Map.class;
					}
					Method setMethod = clazz.getMethod(setMethodName, fieldClazz);
					setMethod.setAccessible(true);
					setMethod.invoke(obj,fieldValue);
					
				}catch(Exception e){
					e.printStackTrace();
					logger.error("******"+obj.getClass().getName()+"设置属性"+fieldStr+"出错！******");
				}
				
			}
		}
	}
	
	public static void setFieldVals(Object obj,String fieldStr,Object... fieldVals){
		if(obj == null || fieldVals == null || StringUtils.isEmpty(fieldStr)){
			return;
		}
		Class[] paramClazz = new Class[fieldVals.length];
		for(int i=0;i<fieldVals.length;i++){
			paramClazz[i] = fieldVals[i].getClass();
		}
		Class clazz = obj.getClass();
		Field[] Fields = clazz.getDeclaredFields();
		for (Field field : Fields) {
			if(fieldStr.equals(field.getName())){
				String initial = fieldStr.charAt(0)+"";
				fieldStr = fieldStr.replaceFirst(initial,initial.toUpperCase());
				String setMethodName = "set"+ fieldStr;
				try{
					Method setMethod = clazz.getMethod(setMethodName, paramClazz);
					setMethod.setAccessible(true);
					setMethod.invoke(obj,fieldVals);
					
				}catch(Exception e){
					e.printStackTrace();
					logger.error("******"+obj.getClass().getName()+"设置属性"+fieldStr+"出错！******");
				}
				
			}
		}
	}
	/**
	 * 得到对象中指定属性的值
	 * @param obj 
	 * @param fieldStr
	 * @return
	 */
	public static Object getFieldValue(Object obj,String fieldStr){
		
		if(null == obj || StringUtils.isBlank(fieldStr)){
			return null;
		}
		Class clazz = obj.getClass();
		String initial = fieldStr.charAt(0)+"";
		fieldStr = fieldStr.replaceFirst(initial,initial.toUpperCase());
		fieldStr = "get" + fieldStr;
		try {
			Method getMethod = clazz.getMethod(fieldStr);
			getMethod.setAccessible(true);
			return getMethod.invoke(obj);
		} catch (Exception e) {
			logger.error("******"+obj.getClass().getName()+"得到属性"+fieldStr+"出错！******");
			return null;
		}
		
	}
	
	
	public static Map<String,String> getFieldAndName(Class clazz){
		
		Field[] declaredFields = clazz.getDeclaredFields();
		Map<String,String> map = new HashMap<String,String>();
		for (Field field : declaredFields) {
			if(field.getModifiers() == 2){
				PropertyAlias annotation = field.getAnnotation(PropertyAlias.class);
				if(null != annotation){
					map.put(annotation.value(), field.getName());
				}
			}
		}
		
		return map;
	}
	
	
	public static void copyProperties(Object dest,Object src){
		Field[] destFields = dest.getClass().getDeclaredFields();
		for (Field destField : destFields) {
			if(2 == destField.getModifiers()){
				String destFieldName = destField.getName();
				Object destFiledValue = null;
				try {
					destFiledValue = getFieldValue(src, destFieldName);
				} catch (Exception e) {
					logger.error(ExceptionUtils.getExceptionStackString(e));
				}
				
				if(null != destFiledValue){
					
					setFiledValue(dest, destFieldName, destFiledValue);
				}
			}
			
		}
	}
	
	public static Map beanToMap(Object obj){
		Field[] declaredFields = obj.getClass().getDeclaredFields();
		return null;
	}
	public static void main(String[] args) throws Exception{
		Router routerNew = new Router();
		Router router = new Router();
		router.setBarDate(DateUtil.formatDate("20160502"));
		router.setBarTime(DateUtil.formatTime("165150"));
		copyProperties(routerNew, router);
		System.out.println(11);
		
		String [] strs = new String[]{"sdd","dsdsd"};
	}
}
